function [CPX] = fractionateAugite_V2(DAT,inputElements,P, KDCPX)




targetElements = {'SiO2' 'TiO2' 'Al2O3' 'Fe2O3' 'FeO' 'MgO' 'CaO' 'Na2O' 'K2O' 'P2O5' 'Cr2O3' 'MnO'}; 
[A,ElementIndicies4Target] = ismember(targetElements, inputElements);
noData = find(ElementIndicies4Target==0); 
ElementIndicies4Target(ElementIndicies4Target == 0) = max(ElementIndicies4Target); 
newlyOrderedElements = DAT(:,ElementIndicies4Target);
%pads rows of NaNs for elements with missing data
newlyOrderedElements(:,noData)=[0]; 
newlyOrderedElements(isnan(newlyOrderedElements))=0; 
DAT = newlyOrderedElements;  



% 1-SIO2 2-TIO2 3-AL2O3 4-FE2O3 5-FEO 6-MGO 7-CAO 8-NA2O 9-K2O 10-P2O5
% 11-CR2O3 12-MNO;
DATmoleweights = [60.085	79.899	101.961	159.69	71.864	40.311	56.079	61.979	94.203	283.89	151.9902	70.9374]; 
MeltMoles =  DAT(1,1:12)./DATmoleweights; %A(4)/A(6); %Fe / Mg in moles
MeltMoles = MeltMoles./sum(MeltMoles).*100; 

XRAT = MeltMoles(5)./MeltMoles(6); 
VAU = KDCPX.*XRAT; 
XPX=VAU/(1.+VAU);

CPX_allPressures = [...
48.95	0.5	1.2	  NaN	 29.71.*XPX	     29.71.*(1-XPX)	19.28	0.3	NaN	NaN	NaN	NaN
48.84	0.5	1.64 NaN	 29.99.*XPX	    29.99.*(1-XPX)	18.74	0.3	NaN	NaN	NaN	NaN
48.67	0.5	2.3	  NaN	  30.41.*XPX	 30.41.*(1-XPX)	17.93	0.3	NaN	NaN	NaN	NaN
48.49	0.5	2.95  NaN	 30.83.*XPX 	30.83.*(1-XPX)	17.12	0.3	NaN	NaN	NaN	NaN];
CPX_allPressures(isnan(CPX_allPressures)) = 0; 
%more al, more FeO+MgO, less Ca with increasing pressure

CPX_allPressures = bsxfun(@times, CPX_allPressures,DATmoleweights); 
CPX_allPressures = bsxfun(@rdivide, CPX_allPressures,nansum(CPX_allPressures,2)).*100;

allPressures = [0 2 5 8]; 

if P >=  allPressures(4)
    CPX = CPX_allPressures(4,:); 
else if P >=  allPressures(3)
    CPX = CPX_allPressures(3,:); 
else if P >=  allPressures(2)
     CPX = CPX_allPressures(2,:); 
else 
     CPX = CPX_allPressures(1,:); 
end
end
end



[A,ElementIndicies4Target] = ismember(inputElements,targetElements);
noData = find(ElementIndicies4Target==0); 
ElementIndicies4Target(ElementIndicies4Target == 0) = max(ElementIndicies4Target); 
newlyOrderedElements = CPX(:,ElementIndicies4Target);
%pads rows of NaNs for elements with missing data
newlyOrderedElements(:,noData)=[0]; 
newlyOrderedElements(isnan(newlyOrderedElements))=0; 
CPX = newlyOrderedElements;  





% % MW = [60.085 79.899	101.961	151.9902	71.846	70.9374	40.311	56.079	61.979	94.203	283.89]; % see matlab guide in excel spreadsheet
% % %     SiO2    TiO2    Al2O3   Cr2O3       FeO     MnO     MgO     CaO    Na2O    K2O    P2O5
% % %      1            2         3          4              5       6           7           8        9
% % 
% % liquidComp_moles = liquidComp./MW;
% % liquidComp_moles = 100*liquidComp_moles/nansum(liquidComp_moles); %%normalizes the molar liquid composition, unneccessary step
% % 
% % XRAT = liquidComp_moles(5)./liquidComp_moles(7);
% % V = FeMgKD*XRAT;
% % XPX = V/(1+V);
% % 
% %     M_eqmin(1) = 48.5;
% %     M_eqmin(2) = 0.75;
% %     M_eqmin(3) = 1.5;
% %     M_eqmin(4) = 0;
% %     M_eqmin(5) = 41.0*XPX;
% %     M_eqmin(6) = 0;
% %     M_eqmin(7) = 41.0*(1-XPX);
% %     M_eqmin(8) = 8.25;
% %     M_eqmin(9) = 0;
% %     M_eqmin(10) = 0;
% %     M_eqmin(11) = 0;
% %     
% % 
% %     eqmin = M_eqmin.*MW;                % this is the bulk comosition of equilibrium augite
% %     eqmin = eqmin./(0.01*sum(eqmin));   % renormalized and in wt%
% %     
% % %       XRAT=A(4)/A(6)
% % %       V=PXK*XRAT
% % %       VO=POK*XRAT
% % %       XOP=VO/(1.+VO)
% % %       XPX=V/(1.+V)
% %       
% %     
% % %        AU(7)=20.60
% % %       AU(4)=28.88*XPX
% % %       AU(6)=28.88*(1.-XPX)
% % %       AU(1)=48.98
% % %       AU(2)=0.97
% % %       AU(3)=0.19
% % %       AU(8)=0.12
% % %SI -AL - TI - FE - P - MG - CA - NA - K - SR - RB - NI
end


